import request from '@/js/request'

const BASE_URL = '/model/config/attributes'

/**
 * 获取模型属性
 * @param modelId 模型Id
 * @param modelAttrFrom 属性所属范围
 * @returns {Promise<AxiosResponse<any>>}
 */
export function getModelAttrs(modelId, modelAttrFrom) {
  return request.get(BASE_URL, {params: {modelId, modelAttrFrom}})
}

/**
 * 删掉为发布的模型属性
 * @param attrIdArr 删除属性Id数组
 * @param modelId 模型Id
 */
export function deleteModelAttrs(attrIdArr, modelId) {
  return request.delete(BASE_URL, {params: {attrIds: attrIdArr.join(','), modelId}})
}

/**
 * 获取属性类型下拉选项中
 * @returns {Promise<AxiosResponse<any>>}
 */
export function getDataTypeOptions() {
  return request.get(BASE_URL + '/getDataTypeOptions')
}

/**
 * 保存更新属性
 * @param attr
 * @param isAdd
 * @param attrClone
 * @returns {Promise<AxiosResponse<any>>}
 */
export function saveAndUpdateModelAttr(attr, isAdd, attrClone) {
  if (isAdd){
    return request.post(BASE_URL, attr, {params: {attrClone}})
  } else {
    return request.put(BASE_URL, attr)
  }
}

/**
 * 属性发布
 * @param modelId
 * @param attrs
 * @returns {Promise<AxiosResponse<any>>}
 */
export function publishedAttrs(modelId, attrs) {
  return request.post(BASE_URL + '/publishedAttrs',
    'modelId=' + modelId + '&attrIds=' + attrs.join(','),
    {
      headers: {'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'}
    }
  )
}

/**
 * 批量添加模型属性
 * @param attrs
 * @param modelId
 */
export function addBatchAttrs(attrs, modelId){
  return request.post(BASE_URL + '/addBatchAttrs', attrs, {params: {modelId}})
}
